package smartcarpool.smartspaceservice;

import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
import sofia_kp.KPICore;
import sofia_kp.iKPIC_subscribeHandler;

import java.net.URI;
import java.util.Vector;

import static smartcarpool.smartspaceservice.SmartSpaceService.*;


public class SmartSpaceSubscribe implements Runnable {

    private SmartSpaceService parent;
	private KPICore smartSpace;
	private String HOST;
	private int PORT;
	private String SPACE_NAME;
	private Vector<Vector<String>> tripleVector;
    private PendingIntent pendingIntent;


	public SmartSpaceSubscribe(String HOST, int PORT, String SPACE_NAME, Vector<Vector<String>> tripleVectorToSubscribe, PendingIntent pendingIntent, SmartSpaceService parent) {
		this.HOST = HOST;
		this.PORT = PORT;
		this.SPACE_NAME = SPACE_NAME;
		this.tripleVector = tripleVectorToSubscribe;
        this.pendingIntent = pendingIntent;
        this.parent = parent;
	}

	@Override
	public void run() {
        String resultSubscription = "";
        try {
            smartSpace = new KPICore(this.HOST, this.PORT, this.SPACE_NAME);
            String joinResult = smartSpace.join();

            if (smartSpace.xmlTools.isJoinConfirmed(joinResult)) {
                smartSpace.setEventHandler(new iKPIC_subscribeHandler() {
                    @Override
                    public void kpic_SIBEventHandler(String s) {
                        Log.d("SmartSpaceSubscription Information Received",s);
                        Vector<Vector<String>> removed_triples = smartSpace.xmlTools.getObsoleteResultEventTriple(s); // Removed & Updated triples
                        Vector<Vector<String>> added_triples = smartSpace.xmlTools.getNewResultEventTriple(s);      // New triples

                        try {
                            pendingIntent.send(parent.getApplicationContext(), RESULT_SUCCESS_SUBSCRIBE, new Intent().putExtra(SMART_SPACE_SUBSCRIBE_NEW_TRIPLES,new TripleList(added_triples)));
                            pendingIntent.send(parent.getApplicationContext(), RESULT_SUCCESS_SUBSCRIBE, new Intent().putExtra(SMART_SPACE_SUBSCRIBE_REMOVED_TRIPLES,new TripleList(removed_triples)));

                        } catch (PendingIntent.CanceledException e) {
                            Log.e("SmartSpaceSubscribe. Pending Intent error while handling subscription, canceled", e.toString());
                            e.printStackTrace();
                        }
                    }
                });
                if (tripleVector.size() == 1){
                    resultSubscription = smartSpace.subscribeRDF(
                            tripleVector.get(0).get(0),
                            tripleVector.get(0).get(1),
                            tripleVector.get(0).get(2),
                            tripleVector.get(0).get(4));
                    if (!smartSpace.xmlTools.isSubscriptionConfirmed(resultSubscription)){
                         pendingIntent.send(RESULT_FAILURE_SUBSCRIBE);
                    }
                }
                Log.d("SmartSpaceSubscribe"," Information has been subscribed");
                smartSpace.leave();
            } else {
                pendingIntent.send(RESULT_FAILURE_JOIN);
            }
        }catch (PendingIntent.CanceledException e){
            Log.e("SmartSpaceSubscribe. Pending Intent Error, Canceled", e.toString());
            e.printStackTrace();
		} catch (Exception e){
            Log.e("SmartSpaceSubscribe. Error", e.toString() + " \nAdditional Information: \n" +
                    "Queried triple: " + tripleVector.toString() + "\n" +
                    "Answer New Triples" + smartSpace.xmlTools.getNewResultEventTriple(resultSubscription).toString()+ "\n" +
                    "Answer Removed Triples" + smartSpace.xmlTools.getObsoleteResultEventTriple(resultSubscription).toString());
            e.printStackTrace();
        }
	}
}
